
// <copyright file="MultipleTests.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The x.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Tests.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using System.IO;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using SubhadraSolutions.Sharp.Utils.Data;
    using SubhadraSolutions.Sharp.Utils.Data.Helpers;
    using SubhadraSolutions.Sharp.Utils.Data.SqlServer;
    using SubhadraSolutions.Sharp.Utils.Diagnostics;
    using SubhadraSolutions.Sharp.Utils.Reflection;
    using SubhadraSolutions.Sharp.Utils.Tests.DummyClasses;

    /// <summary>
    /// The x.
    /// </summary>
    public class X
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        public decimal Value { get; set; }

        #endregion Public Properties
    }

    /// <summary>
    /// The multiple tests.
    /// </summary>
    [TestClass]
    public class MultipleTests
    {
        #region Public Methods and Operators

        /// <summary>
        /// The test_ dynamic string separated export helper_and_ data reader helper.
        /// </summary>
        [TestMethod]
        public void Test_DynamicStringSeparatedExportHelper_and_DataReaderHelper()
        {
            var employees1 = new List<Employee>(buildDummyEmployees(1000));

            var sw = new StringWriter();
            DynamicStringSeparatedExportHelper<Employee>.WriteToTextWriter(employees1, sw, "\t", true);
            string s = sw.ToString();
            sw.Dispose();

            var sr = new StringReader(s);
            IDataReader reader = DataReaderHelper.GetDataFromCharSeparatedTextReader(sr, '\t');

            IEnumerable<Employee> enumerable = reader.ConvertDataReaderToObjects<Employee>();
            var employees2 = new List<Employee>(enumerable);
            sr.Close();

            bool result = compareEmployees(employees1, employees2);
            Assert.IsTrue(result);
        }

        /// <summary>
        /// The test_ dynamic string separated export helper_and_ data reader helper 1.
        /// </summary>
        [TestMethod]
        public void Test_DynamicStringSeparatedExportHelper_and_DataReaderHelper1()
        {
            long ticksBefore1 = SharedStopWatch.ElapsedTicks;
            const int empCount = 100000;
            IEnumerable<Employee> employees1 = buildDummyEmployees(empCount);
            IDataReader reader1 = employees1.ExportAsDataReader();
            ExcelHelper.WriteDataReader(reader1, @"C:\Temp\EX1.xlsx", "Data");
            long ticksTaken1 = SharedStopWatch.ElapsedTicks - ticksBefore1;

            long ticksBefore2 = SharedStopWatch.ElapsedTicks;
            IEnumerable<Employee> employees2 = buildDummyEmployees(empCount);
            IDataReader reader2 = employees2.ExportAsDataReader();
            var bufferedReader = new BufferedDataReader(reader2, 100);
            ExcelHelper.WriteDataReader(bufferedReader, @"C:\Temp\EX2.xlsx", "Data");
            long ticksTaken2 = SharedStopWatch.ElapsedTicks - ticksBefore2;
            long diff = ticksTaken1 - ticksTaken2;

            TimeSpan ts1 = TimeSpan.FromTicks(ticksTaken1);
            TimeSpan ts2 = TimeSpan.FromTicks(ticksTaken2);
            TimeSpan ts3 = TimeSpan.FromTicks(diff);
        }

        /// <summary>
        /// The test_ x.
        /// </summary>
        [TestMethod]
        public void Test_X()
        {
            var list = new List<X>();
            for (int i = 0; i < 10; i++)
            {
                var x = new X { Name = "Name" + i, Value = (decimal)(i + 0.5) };
                list.Add(x);
            }

            // IDataReader reader = list.ExportAsDataReader();
            const string constr = "Data Source=.;Initial Catalog=B;Integrated Security=True";
            using (var conn = new SqlConnection(constr))
            {
                conn.Open();
                BulkCopier.Instance.DoBulkCopy(conn, list);
            }
            // DynamicDAO<X>.Insert(list, conn);
            // DataTable table = reader.ExportAsDataTable("Data");
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// The build dummy employees.
        /// </summary>
        /// <param name="count">
        /// The count.
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable"/>.
        /// </returns>
        private static IEnumerable<Employee> buildDummyEmployees(int count)
        {
            for (int i = 0; i < count; i++)
            {
                Employee e = Employee.BuildDummyEmployee();
                e.Id = i;
                e.FirstName += i.ToString(CultureInfo.InvariantCulture);
                e.LastName += i.ToString(CultureInfo.InvariantCulture);
                e.DateOfBirth = e.DateOfBirth.AddDays(i);
                e.DateOfJoin = e.DateOfJoin.AddDays(i);
                e.EmploymentType = i % 2 == 0 ? EmploymentType.FullTime : EmploymentType.PartTime;
                yield return e;
            }
        }

        /// <summary>
        /// The compare employees.
        /// </summary>
        /// <param name="employees1">
        /// The employees 1.
        /// </param>
        /// <param name="employees2">
        /// The employees 2.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private static bool compareEmployees(IList<Employee> employees1, IList<Employee> employees2)
        {
            if (employees1.Count != employees2.Count)
            {
                return false;
            }

            for (int i = 0; i < employees1.Count; i++)
            {
                if (!employees1[i].Equals(employees2[i]))
                {
                    return false;
                }
            }

            return true;
        }

        #endregion Methods
    }
}